﻿using AILabelTool.Models;
using AILabelTool.Models.DrawShapes;
using AILabelTool.Models.Enums;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AILabelTool.CustomControls
{
    /// <summary>
    /// DrawImageRect.xaml 的交互逻辑
    /// </summary>
    public partial class DrawImageRect : MetroContentControl
    {
        /// <summary>
        /// 当前的绘制状态
        /// </summary>
        public static readonly DependencyProperty CurrentDrawTypeProperty = DependencyProperty.Register("CurrentDrawType", typeof(ShapeType), typeof(DrawImageRect), new PropertyMetadata(ShapeType.None));
        /// <summary>
        /// 模型
        /// </summary>
        public ImageContentInfoModel? ImageModel => this.DataContext as ImageContentInfoModel;
        /// <summary>
        /// 当前操作的类型
        /// </summary>
        public ShapeType CurrentDrawType
        {
            get => (ShapeType)GetValue(CurrentDrawTypeProperty);
            set => SetValue(CurrentDrawTypeProperty, value);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public DrawImageRect()
        {
            InitializeComponent();

            this.Loaded += DrawImageRect_Loaded;
        }

        private void DrawImageRect_Loaded(object sender, RoutedEventArgs e)
        {
            DrawImageBox.SizeChanged += DrawImageBox_SizeChanged;
            this.DrawCanvas.MouseDown += DrawCanvas_MouseDown;
            this.DrawCanvas.MouseMove += DrawCanvas_MouseMove;
            //this.DrawCanvas.MouseLeave += DrawCanvas_MouseLeave;
            this.DrawCanvas.MouseUp += DrawCanvas_MouseUp;
            

        }

        /// <summary>
        /// 尺寸变更
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void DrawImageBox_SizeChanged(object sender, SizeChangedEventArgs e)
        {

        }





        #region 鼠标控制
        private List<Line> PolygonShape = new List<Line>();
        private Rectangle RectangleShape = new Rectangle();
        private Line CurrentLin;
        private bool IsDrawPolygon = false;
        private bool IsDrawBox = false;
        /// <summary>
        /// 抬起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawCanvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if(this.ImageModel is null) return;
            if (e.ChangedButton == MouseButton.Left)
            {
                var point = e.GetPosition(DrawCanvas);
                if (IsDrawPolygon)
                {
                    CurrentLin.X2 = point.X;
                    CurrentLin.Y2 = point.Y;
                }
                else if (CurrentDrawType == ShapeType.Box && IsDrawBox)
                {
                    // 创建形状
                    var box = new ShapeBox()
                    {
                        X = Canvas.GetLeft(RectangleShape),
                        Y = Canvas.GetTop(RectangleShape),
                        Width = RectangleShape.Width,
                        Height = RectangleShape.Height,
                        TypeName = MainWindow.Instance.MainModel.ToolConfig.GetCurrentTypeName().TypeName,
                    };
                    //过滤面积过小的选择框
                    if(box.Width * box.Height > 15)
                    {
                        ImageModel.Shapes.Add(box);
                    }
                    // 删除选择框
                    DrawCanvas.Children.Remove(RectangleShape);
                    IsDrawBox = false;
                    RectangleShape.Width = 1;
                    RectangleShape.Height = 1;
                }
            }
            else if (e.ChangedButton == MouseButton.Right)
            {
                //如果是绘制多边形的任务下
                if (IsDrawPolygon)
                {
                    IsDrawPolygon = false;
                    CurrentLin.X2 = this.PolygonShape.First().X1;
                    CurrentLin.Y2 = this.PolygonShape.First().Y1;
                    if (PolygonShape.Count > 1)
                    {
                        var res = ConvertToShapePolygon();
                        this.ImageModel.Shapes.Add(res);
                    }
                    ClearPolygonLines();
                    IsDrawPolygon = false;
                }
            }
        }


        /// <summary>
        /// 按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (this.ImageModel is null || e.ChangedButton != MouseButton.Left) return;
            var point = e.GetPosition(DrawCanvas);
            if (CurrentDrawType == ShapeType.None) return;
            if(CurrentDrawType == ShapeType.Polygon && IsDrawPolygon is false)
            {
                //创建line
                CurrentLin = new Line();
                CurrentLin.X1 = point.X;
                CurrentLin.Y1 = point.Y;
                CurrentLin.X2 = point.X;
                CurrentLin.Y2 = point.Y;
                CurrentLin.Stroke = Brushes.Red;
                CurrentLin.StrokeThickness = 2;
                DrawCanvas.Children.Add(CurrentLin);
                PolygonShape.Add(CurrentLin);
                IsDrawPolygon = true;
            }
            else if (IsDrawPolygon)
            {
                //赋值末尾
                CurrentLin.X2 = point.X;
                CurrentLin.Y2 = point.Y;

                CurrentLin = new Line();
                CurrentLin.X1 = point.X;
                CurrentLin.Y1 = point.Y;
                CurrentLin.X2 = point.X;
                CurrentLin.Y2 = point.Y;
                CurrentLin.Stroke = Brushes.Red;
                CurrentLin.StrokeThickness = 2;
                DrawCanvas.Children.Add(CurrentLin);
                PolygonShape.Add(CurrentLin);
            }
            else if (CurrentDrawType == ShapeType.Box && IsDrawBox == false)
            {
                DrawCanvas.Children.Add(RectangleShape);
                Canvas.SetLeft(RectangleShape, point.X);
                Canvas.SetTop(RectangleShape, point.Y);
                RectangleShape.StrokeThickness = 2;
                RectangleShape.Stroke = Brushes.Red;
                IsDrawBox = true;
            }
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.ImageModel is null) return;
            if (!IsDrawBox && !IsDrawPolygon) return;
            var point = e.GetPosition (DrawCanvas);
            if (IsDrawPolygon)
            {
                CurrentLin.X2 = point.X;
                CurrentLin.Y2 = point.Y;
            }
            else if (CurrentDrawType == ShapeType.Box && IsDrawBox)
            {
                var p = new Point(Canvas.GetLeft(RectangleShape), Canvas.GetTop(RectangleShape));
                double x = Math.Min(point.X, p.X);
                double y = Math.Min(point.Y, p.Y);

                RectangleShape.Width = Math.Abs(point.X - p.X);
                RectangleShape.Height = Math.Abs(point.Y - p.Y);
                Canvas.SetLeft(RectangleShape, x);
                Canvas.SetTop(RectangleShape, y);
            }
        }
        /// <summary>
        /// 丢失鼠标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void DrawCanvas_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this.ImageModel is null) return;
            if (IsDrawPolygon)
            {
                this.DrawCanvas.Children.Remove(CurrentLin);
                this.PolygonShape.Remove(CurrentLin);
                ClearPolygonLines();
                IsDrawPolygon = false;
            }
            else if (CurrentDrawType == ShapeType.Box && IsDrawBox)
            {
                DrawCanvas.Children.Remove(RectangleShape);
                IsDrawBox = false;
            }
        }

        /// <summary>
        /// 转换成多边形
        /// </summary>
        /// <returns></returns>
        private ShapePolygon ConvertToShapePolygon()
        {
            var x = double.MaxValue;
            var y = double.MaxValue;
            foreach (var item in PolygonShape)
            {
                if(item.X1 < x)
                {
                    x = item.X1;
                }
                if (item.Y1 < y)
                {
                    y = item.Y1;
                }
            }

            ShapePolygon shapePolygon = new ShapePolygon();
            shapePolygon.StartX = x;
            shapePolygon.StartY = y;
            shapePolygon.TypeName = MainWindow.Instance.MainModel.ToolConfig.GetCurrentTypeName().TypeName;
            foreach (var item in PolygonShape)
            {
                shapePolygon.Points.Add(new Point(item.X1 - x, item.Y1 - y));
            }

            return shapePolygon;
        }
        /// <summary>
        /// 清除当前的辅助线
        /// </summary>
        private void ClearPolygonLines()
        {
            foreach (var item in PolygonShape)
            {
                this.DrawCanvas.Children.Remove(item);
            }
            PolygonShape.Clear();
        }
        #endregion
    }
}
